package com.whsxt.util;

import com.alibaba.fastjson.JSON;
import com.whsxt.common.Result;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author 武汉尚学堂
 * 将数据使用JSON的格式返回给客户端
 */
public abstract class RespUtil {


    /**
     * 将 result 对象转化为JSON 进行输出
     * @param response
     * @param rs
     */
    public static void writer(HttpServletResponse response, Result rs) throws IOException {
        response.setContentType("text/json;charset=utf-8");
        PrintWriter writer = response.getWriter();
        String s = JSON.toJSONString(rs);
        writer.print(s);
        writer.flush();
        writer.close();
    }
}
